package com.fgs.common.progress;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;

import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.CustomProgressDialog;


/**
 * @author fgs
 * @date 2020/3/27
 **/

/**
 * Dialog的进度控制
 */

public class ProgressDialogHandler extends Handler {
    private CustomProgressDialog dialog;
    private Context context;
    private boolean cancelable;
    private ProgressCancelListener mProgressCancelListener;

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener, boolean cancelable) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
        initProgressDialog();
    }

    private void initProgressDialog() {
        if (dialog == null) {
            dialog = new CustomProgressDialog(context);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(cancelable);
            if (cancelable) {
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }
        }
        showProgressDialog();
    }

    public void showProgressDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismissProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
